package cn.gov.pbc.tims.controller;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.internal.util.SerializationHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gov.pbc.tims.entity.User;
import cn.gov.pbc.tims.util.Operator;
import cn.gov.pbc.tims.util.Result;

@Controller  
@RequestMapping("/login")  
public class LoginController {  

	private static final Logger LOGGER = LogManager.getLogger(LoginController.class);
	@Autowired
	private Operator operator;
	@RequestMapping("")  
	public @ResponseBody Result  login(String usercode,String password,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {  
		Result result=new Result(); 
		User op = operator.authenticate(usercode, password);
		if(operator.isLogin()){
			LOGGER.info(op.getName()+" 成功登录，usercode:"+op.getCode()); 
			result.setValue(0, "成功登录", op);
			Cookie usernameCookie = new Cookie("username", URLEncoder.encode(op.getCode(), "UTF-8"));
            Cookie pwdCookie = new Cookie("pwd", URLEncoder.encode(op.getPassword(), "UTF-8"));
            usernameCookie.setPath("/");  
			pwdCookie.setPath("/");
            usernameCookie.setMaxAge(60 * 60 * 24);
            pwdCookie.setMaxAge(60 * 60 * 24);
            response.addCookie(usernameCookie);
            response.addCookie(pwdCookie);
			
		}else{
			LOGGER.info("登录失败，usercode:"+usercode);
			result.setValue(1, "登录失败！用户代码或者口令错。");
		};
		return result;  
	}
	@RequestMapping("/logout")
	public @ResponseBody Result logout(HttpServletResponse response,HttpServletRequest request){
		Result result=new Result();
		User op=operator.getUser();
		if(op==null){
			LOGGER.info("登出操作，无登录用户");
			result.setValue(0, "登出操作，无登录用户");
		}else{
			LOGGER.info(op.getName()+" 成功退出登录");
			Cookie usernameCookie=new Cookie("username",null); 
			Cookie pwdCookie=new Cookie("pwd",null); 
			//设置cookie存活时间,单位秒 
			usernameCookie.setMaxAge(0); 
			pwdCookie.setMaxAge(0); 
			usernameCookie.setPath("/");  
			pwdCookie.setPath("/");
			// 将用户名和密码存放到客户端的Cookie中 
			response.addCookie(usernameCookie); 
			response.addCookie(pwdCookie); 
			operator.logout();
		

			result.setValue(0, op.getName()+" 成功退出登录");
		}		
		return result;
	}
	@RequestMapping(value="/getop",produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody Result getop(){
		Result result=new Result();
		LOGGER.info("获得操作员信息");
		User op= (User)SerializationHelper.clone(operator.getUser());		
		if(op!=null){
			op.setPassword("......");
			result.setValue(0, op);
		}else{
			result.setValue(1, "尚未登录");
		}		
		return result;
	}
}  
